* -----------------------------------------------------------------
*                     Variable Transformation 
* >> based on lag structure, control sets and approach 
* >> specify pLags & qLags & controlist | controlist1 & controlist2
* >> & approach
* -----------------------------------------------------------------

keep if inrange(Date, tq(${sample_start}), tq(${sample_end}))  

if $skipsample == 1 {
	drop if inrange(Date,  tq(${skip_start}q1), tq(${skip_end}q4) )
}

tsset Date, quarterly
** lagged shocks
qui forv q = 1/$qLags {
	gen l`q'_X_TaxcutToGDP = l`q'.X_TaxcutToGDP
}
** lagged endogenous vars 
*** if only vars that take lags for differenced vars 
if !missing("$controlist") {
	forv p = 1/$pLags {
		foreach var in $controlist {			
				gen l`p'd_`var' = l`p'.d.`var'
		}
	}
}

*** if vars consist of both taking lags only and lagged diff
else if missing("$controlist") {
	**** take lags only
	forv p = 1/$pLags {
		foreach var in $controlist1 {
			gen l`p'_`var' = l`p'.`var'
		}
	}
	**** take lagged diff 
	forv p = 1/$pLags {
		foreach var in $controlist2 {			
				gen l`p'd_`var' = l`p'.d.`var'
		}
	}
}

if ${spendControl} == 1 {
	
	if missing("$gLags") {	
		global gLags = $pLags		
	}
	
	forv g = 0/$gLags {		
			gen l`g'_${spendControlVar} = l`g'.${spendControlVar} 
			gen f`g'_${spendControlVar} = f`g'.${spendControlVar}
	}	
}

* ---------------------------------------------------------------------- *

* Long difference on the LHS
local hirf = $hmax - 1         // IRF horizon (including 0)

qui forv h = 0/`hirf' {

	cap drop ${depvar}`h'

	 if "${depvar}" == "ATR" {
			gen ATR`h' = f`h'.TaxRatio - l.TaxRatio			
	}
	
	else if "${depvar}" == "DefToGDP" {
			gen DefToGDP`h' = (f`h'.RealDef - l.RealDef)*100/(l.RealGDP)			
	}
	
	else {		
		gen ${depvar}`h' = f`h'.${depvar} - l.${depvar}
	}

}	


* ---------------------------------------------------------------------- *

if "${approach}" == "LP" {

	if "${LHS}" == "smoothed" {

		forvalues h=0/`hirf' {

			cap drop f`h'${depvar}smooth

			if `h' == 0 | `h' == `hirf' {

				qui: gen f`h'${depvar}smooth = ${depvar}`h'

			}
			if inrange(`h', 1, `hirf' - 1) {

				local h1 = `h' - 1
				local h2 = `h' + 1

				qui: gen f`h'${depvar}smooth = (${depvar}`h1' +  ${depvar}`h' + ${depvar}`h2')/3
			}
		}
	}

}


* ---------------------------------------------------------------------- *
/*  sample size harmonization */

if ${harmonized} == 1 {
	* Harmonize sample to get consistent measure
	keep if inrange(Date, tq(${sample_start}), tq(${sample_end}) - `hirf' )   
}
else {
	* Sample is not harmonized
	keep if inrange(Date, tq(${sample_start}), tq(${sample_end}) )   
}

if $skipsample == 1 & $skip_harmonized == 1 {
	drop if inrange(Date,  tq(${skip_start}q1) - `hirf' , tq(${skip_start}q1) )
}

